#!/usr/bin/env python

"""
    Example 1: Read values from a number of analog and digital sensors and control
    one or more servos.
    
    The Pi Robot Project: http://www.pirobot.org
    Copyright (c) 2010 Patrick Goebel.  All right reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details at:
    
    http://www.gnu.org/licenses/gpl.html
      
    NOTE: See the offical ElementTM manual at:
    http://www.roboticsconnection.com/multimedia/docs/Element_3.0_UserGuide.pdf
"""

from element_driver import Element
import time, os
    
if os.name == "posix":
    portName = "/dev/ttyUSB0"   # Change this to your main Element port!   
    #portName = "/dev/rfcomm0" # For bluetooth on Linux
    # Note: On Linux, after connecting to the Bluetooth adapter, run the command
    # sudo rfcomm bind /dev/rfcomm0
else:
    portName = "COM12" # Change this to your main Element's port!
    
baudRate = 57600 # Change this to your Element's baud rate!

myElement = Element(port=portName, baudrate=baudRate, timeout=0.5, units=0)
myElement.connect()
    
print "Firmware Version", myElement.fw()
print "Units", myElement.get_units()
print "Baudrate", myElement.get_baud()
    
# Uncomment the following line to move a servo on pin 5 to position 0
#print "Moving servo on GPIO pin 5 (servo ID 6)", myElement.servo(6, 0)

samples = 1

for i in range(samples):
    print "All raw analog sensor values:", myElement.get_all_analog()
    print "Raw analog values from the cache:", myElement.analog_sensor_cache
    print "Element voltage from the cache", myElement.voltage(cached=True)
    print "Ping sonar reading on digital pin 0:", myElement.get_Ping(0)
    print "Ping reading from the cache:", myElement.get_Ping(0, cached=True)
    print "Sharp IR reading using on analog pin 0:", myElement.get_GP2D12(0)
    print "Sharp IR reading from cache:", myElement.get_GP2D12(0, cached=True)
    #print "MaxEZ1 on pins 6 and 7", myElement.get_maxez1(6, 7)
    time.sleep(0.05)
    
print "\nTesting completed, shutting down."

myElement.stop()
myElement.close()

